package com.shopping.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.shopping.common.entity.User;
import com.shopping.common.entity.resp.JwtResponse;
import com.shopping.common.entity.vo.UserVo;
import com.shopping.common.result.Result;
import com.shopping.common.utils.StringUtils;
import com.shopping.system.service.UserService;
import com.shopping.system.service.impl.InformationImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Log4j2
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    /**
     * 查找用户
     * @return
     */
    @PostMapping("/findUserByUsername/{username}")
    public Result<UserVo> findUserByUsername(@PathVariable String username){

        log.info("请求路径：【{}】，请求方式：【{}】，请求参数：【{}】",
                request.getRequestURI(),request.getMethod(), username);

        Result<UserVo> result = userService.findUserByUsername(username);

        log.info("请求路径：【{}】，请求方式：【{}】，响应结果：【{}】",
                request.getRequestURI(),request.getMethod(), result);

        return result;
    }

    /**
     * 添加用户
     * @param userVo
     * @return
     */
    @PostMapping("/addUser")
    public Result<String> addUser(@RequestBody UserVo userVo){
        log.info("请求路径：【{}】，请求方式：【{}】，请求参数：【{}】",
                request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(userVo));

        Result<String> result=userService.addUser(userVo);
        log.info("请求路径：【{}】，请求方式：【{}】，响应结果：【{}】",
                request.getRequestURI(),request.getMethod(), result);

        return result;
    }

    /**
     * 是否登录
     * @return
     */
    @PostMapping("/isLogin")
    public Result<UserVo> isLogin(){
        log.info("请求路径：【{}】，请求方式：【{}】",
                request.getRequestURI(),request.getMethod());
        UserVo userLogin = InformationImpl.findUserLogin(redisTemplate, request);
        Result<UserVo> result = Result.success(userLogin);
        log.info("请求路径：【{}】，请求方式：【{}】，响应结果：【{}】",
                request.getRequestURI(),request.getMethod(), result);
        return result;
    }

}
